<?php
/**
 * 节点模型
 * ============================================================================
 * 版权所有 2017 福建华晟网络科技，并保留所有权利。
 * 网站地址: http://www.fldcms.cn
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和使用 .
 * 不允许对程序代码以任何形式任何目的的再发布。
 * ============================================================================
 * Author: fld
 * Date: 2017-07-01
 */
namespace app\common\model;
class NodeModel extends BaseModel {
	private $table_name = 'node';
	
	/**
	 * 获取节点列表
	 * @param array $condition
	 */
	public function getNodeList($condition){
		$order = "sort asc";
		$list = $this->select_table($this->table_name,$condition,'*',$order);
		return $list;
	}
	/**
	 * 根据pid来获取节点列表
	 * @param int $pid  父节点
	 * @param int $is_menu 0、节点，1、菜单，2、所有
	 */
	public function getNodes($pid,$is_menu){
		$conds = array();
		$conds['pid'] = $pid;
		if($is_menu == 1 || $is_menu == 0){
			$conds['is_menu'] = $is_menu;
		}
		$order = "sort asc";
		$nodes = $this->select_table($this->table_name,$conds,'*',$order);
		if(is_array($nodes) && !empty($nodes)){
			foreach($nodes as $key=>$val){
				$nodes[$key]['access_val'] = $val['node_controll']."_".$val['node_action'];
				$pid = $val['node_id'];
				$cmenus = $this->getNodes($pid,$is_menu);
				foreach ($cmenus as $k=>$v){
					$cmenus[$k]['access_val'] = $v['node_controll']."_".$v['node_action'];
				}
				if(is_array($cmenus) && !empty($cmenus)){
					$nodes[$key]['c_list'] = $cmenus;
				}
			}
		}
		return $nodes;
	}
	/**
	 * 添加节点
	 */
	public function addData(){
		$data = array();
    	$data['cate_name'] = input('post.cate_name');
    	if(empty(input('post.cate_keyword'))){
    		$data['cate_keyword'] = input('post.cate_name');
    	}else{
    		$data['cate_keyword'] = input('post.cate_keyword');
    	}
    	if(empty(input('post.cate_description'))){
    		$data['cate_description'] = input('post.cate_name');
    	}else{
    		$data['cate_description'] = input('post.cate_description');
    	}
    	$data['sort'] = input('post.sort');
    	$data['update_time'] = time();
		$res = $this->add_table($this->table_name,$data);
		if($res !== false){
			model('ActionLog')->action_log("添加","添加节点成功，节点编号=".$res."，节点名称=".input('post.cate_name'));
			return $res;
		}else{
			return false;
		}
	}
    /**
     * 获取单个节点信息
     * @param unknown $condition
     */
	public function getCateInfo($condition){
		return $this->find_table($this->table_name,$condition);
	}
	/**
	 * 更新节点信息
	 */
	public function updateData(){
		$id = input('post.id');
		$data['cate_name'] = input('post.cate_name');
		$data['cate_keyword'] = input('post.cate_keyword');
		$data['cate_description'] = input('post.cate_description');
		$data['sort'] = input('post.sort');
		$res = $this->update_table($this->table_name,array('id'=>$id),$data,1);
		if($res !== false){
			model('ActionLog')->action_log("编辑","编辑节点成功，节点编号=".$id);
			return $res;
		}else{
			return false;
		}
	}
	/**
	 * 删除节点信息
	 */
	public function deleteData(){
		$id = input('param.id');
		$cate_info = $this->find_table($this->table_name,array('id'=>$id),'id,cate_name');
		$res = $this->delete_table($this->table_name,array('id'=>$id));
		if($res !== false){
			model('ActionLog')->action_log("删除","删除节点成功，节点编号=".$cate_info['id']."，节点账号=".$cate_info['cate_name']);
			return $res;
		}else{
			return false;
		}
	}
}
